import { View, Text, Image, Input, Textarea } from "@tarojs/components";
import Taro, { useDidShow, useLoad } from "@tarojs/taro";
import { Button, Cell, CellGroup, Empty, NavBar, Overlay, Picker } from "@nutui/nutui-react-taro";
import { Share, More, Cart, ArrowLeft, Close } from '@nutui/icons-react-taro'
import { c } from "../../common";
import { useEffect, useState } from "react";
import { getParams, navigateTo } from "../../common/_";
import { selectExamDetail, startExam } from "../../api/ceshi";
import SelectItem from "../../components/selectItem";
import InputItem from "../../components/inputItem";
import CustomCell from "../../components/custom-cell";
import dayjs from "dayjs";
import { iconCloseRed, iconSuccessGreen, iconWait, iconWaitBlue } from "../../assets/files";
import { audit, getTrainingInfoWait, selectHiddenById } from "../../api/apply";
import { getSystemDictValue, getSystemEnumValue } from "../../utils/globalData";
import { baseUrl } from "../../utils/request";
import "./index.scss";


export default function Mine() {

  const [trainingTime, setTrainingTime] = useState("")
  const [auditReason, setAuditReason] = useState("")

  const [auditType, setAuditType] = useState("")
  const [detail, setDetail] = useState({})
  const [shenqStatus, setShenqStatus] = useState([
    {
      id: 2,
      name: "未分享",
      color: '#FF1616',
      bgColor: 'rgba(255, 148, 148, 0.5)',
      icon: iconWait
    },
    {
      id: 1,
      name: "已分享",
      color: 'rgba(7, 193, 96, 1)',
      bgColor: 'rgba(176, 255, 214, 0.35)',
      icon: iconSuccessGreen
    },
  ]);

  useEffect(() => {
    getDetail(getParams().id)
  }, [])

  const getDetail = async (id) => {
    const { success, data } = await selectHiddenById({ id })
    if (success) {
      setDetail(data)
    }
  }

  

  function matchType(type) {
    for (let index = 0; index < shenqStatus.length; index++) {
      const element = shenqStatus[index];
      if (element.id === type) {
        return element
      }
    }
  }

  const shenhe = (type) => {
    setAuditType(type)
    setTrainingTime(detail.trainingTime)
    setSuccessVisible(true)
  }

  const changePicker = (list, option, columnIndex) => {
    console.log(columnIndex, option)
  }

  const auditSubmit = async (leturerName, leturer) => {

    // Taro.navigateBack()
    //   Taro.eventCenter.trigger('reAuditList', {});
    //   return;

    const { success } = await audit({
      auditReason,
      auditState: auditType,
      id: detail?.id,
      leturer,
      // leturerName,
      trainingTime: trainingTime.length === 19 ? trainingTime : dayjs().format('YYYY-')+trainingTime+":00"
    })

    if (success) {
      setSuccessVisible(false)
      Taro.navigateBack()
      Taro.eventCenter.trigger('reAuditList', {});
      Taro.showToast({
        title: '提交成功',
        icon: 'none'
      })
    } else {
      Taro.showModal({
        content: msg
      })
    }
  }

  const previewImage = (curUrl) => {
    let arr = []
    detail.fileUrls.forEach(v => {
      arr.push(baseUrl + v)
    })
    Taro.previewImage({
      current: curUrl, // 当前显示图片的http链接
      urls: arr, // 需要预览的图片http链接列表
    });
  }

  return (
    <View className="wrap">
      <View className="topContent">
        <Image src={detail?.isShare && matchType(detail?.isShare).icon} className="waitSty" />
        <Text className="shenqingStatus">
          {detail?.isShare && matchType(detail?.isShare).name}
        </Text>
      </View>
      <View className="formWrap">
        
        <CustomCell label="辖区" value={detail?.jurisdiction} />
        <CustomCell label="联系电话" value={detail?.phone} valueStyle={{ color: 'blue' }} onClick={() => { Taro.makePhoneCall({ phoneNumber: detail?.phone }) }} />
        <CustomCell label="上报时间" value={detail?.createdAt} />
        <View className="iclodWrap">
          <Text className="icLabel">隐患内容</Text>
          <Text className="icValue">{detail?.content}</Text>
        </View>
        <View className="iclodWrap">
          <Text className="icLabel">隐患位置</Text>
          <Text className="icValue">{detail?.location}</Text>
        </View>
        <View className="picWrap">
            <Text className="pictit">图片</Text>
            <View className="imgListWrap">
              {
                detail?.fileUrls?.map(v => {
                  return (
                    <Image onClick={() => previewImage(baseUrl + v)} className="imgShow" src={baseUrl + v} />
                  )
                })
              }
            </View>
          </View>

      </View>

   

      
    </View>
  );
}
